﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
//using System.Web.HttpContext.Current.Request.Files;
using NuGet.Protocol;
using SubSonic.Sugar;
using System.Collections.Generic;
using System.Text;

namespace hnrzxt.Controllers
{
    public class ZlxxesController : Controller
    {

        private readonly ApplicationDbContext _db;
        public ZlxxesController(ApplicationDbContext db)
        {
            _db = db;
        }

        [HttpPost]
        public string Create(Zlxx zlxx)
        {

            if (ModelState.IsValid)
            {
                _db.Add(zlxx);
                _db.SaveChanges();
            }
            return "";
        }

        public IActionResult Index()
        {
            return View();
        }

        public List<Dictionary<string, object>> ShowZlxxList(string Jbxxid)
        {
            List<Zlxx> zlxxs = _db.zlxxes.Where(p => p.Jbxxid == Jbxxid).ToList();
            
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            for (int i = 0; i < zlxxs.Count; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                map.Add("id", zlxxs[i].Id);
                map.Add("lyscrouce", zlxxs[i].Lyscrouce);
                map.Add("remark", zlxxs[i].Remark);
                map.Add("rz", zlxxs[i].Rz);
                map.Add("rcdjb", zlxxs[i].Yrbm);
                map.Add("msb", zlxxs[i].Msb);
                map.Add("bjdc", zlxxs[i].Bjdc);
                map.Add("result", zlxxs[i].Result);
                map.Add("resultBz", zlxxs[i].ResultBz);
                list.Add(map);
            }
            return list;
        }

        public Dictionary<string, object> ShowZlxx(string Jbxxid)
        {
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add("code", 0);
            map.Add("data", ShowZlxxList(Jbxxid));


            return map;
        }

        public string Del_zlxx(int id)
        {
            try
            {
                var zlxx = _db.zlxxes.Find(id);
                if (zlxx == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.zlxxes.Remove(zlxx);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }        


        //public string Update(int id)
        //{

        //}
    }
}